package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;

public class ImageToggleButton extends JToggleButton implements MouseListener {

	private static final Color BACKGROUND = new Color(255, 255, 255, 128);
	private static final Color MOUSEOVER = new Color(255, 255, 0, 196);
	private static final Color SELECTED = new Color(255, 255, 255, 196);
	private static final Color MOUSEDOWN = new Color(255, 255, 96, 128);
	private static final Insets NOINSETS = new Insets(2, 2, 2, 2);
	
	private static final Color HIGHLIGHT = new Color(255, 255, 255, 196);
	private static final Color SHADOW = new Color(0, 0, 0, 128);
	private BufferedImage icon;
	
	private boolean mouseOver = false;
	private boolean mouseDown = false;
	
	public ImageToggleButton(BufferedImage icon) {
		super(new ImageIcon(icon));
		this.icon = icon;
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, HIGHLIGHT, SHADOW));
		this.setOpaque(false);
		this.setMargin(NOINSETS);
		this.addMouseListener(this);
	}
	
	@Override
	public void paintComponent(Graphics g) {
		if (this.isSelected()) {
			g.setColor(SELECTED);
		} else if (mouseDown) {
			g.setColor(MOUSEDOWN);
		} else if (mouseOver) {
			g.setColor(MOUSEOVER);
		} else {
			g.setColor(BACKGROUND);
		}
		g.fillRect(0, 0, icon.getWidth(), icon.getHeight());
		if (mouseDown)
			g.drawImage(icon, 1, 1, null);
		else
			g.drawImage(icon, 0, 0, null);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		mouseOver = true;
		repaint();
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		mouseOver = false;
		repaint();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		mouseDown = true;
		repaint();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		mouseDown = false;
		repaint();
	}
	
}
